package tools;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
/*
 * 
 * 改进的pagebean，传入一句HQL语句完成翻页查询的工作，并且返回相关的页脚,在页脚中记录首页，尾页，上一页，下一页的页码。
 * 同时返回一个list对象可以提供返回到页面上做展示的需求。适合使用于hibernate和一般的jdbc。如果是一般的hibernate链接
 * 请配置好相应的参数；如果是jdbc链接请配置好驱动程序，并将用户名和密码配置在属性文件中。
 */
public class PageBeanPlus {
	private String SqlType;// hql的查询语句
	private String Sql;// sql的查询语句
	private Integer pagesize;// 页面大小
	private Integer maxpage;// 最大页
	private Integer currentpage;// 当前页
	private Integer nextpage;// 下一页
	private Integer previouspage;// 上一页
	private List<Integer> pagelist; // 按照页面展示一个可跨页选择的列表
	private String database;// 数据库类型
	private String formatquery;// 整理后的数据
	private String pagefoot;// 需要在页脚显示的内容
	private String pagefoot_ajax;
	private String ajaxfunction;
	@SuppressWarnings("unchecked")
	private List formatquerylist;
	private ResultSet formatqueryresultset;
	public void WrongFormat() {
		// 参数错误是定义pagebean
		this.SqlType = "";
		this.Sql = "";
		this.pagesize = 0;
		this.maxpage = 0;
		this.currentpage = 0;
		this.nextpage = 0;
		this.previouspage = 0;
		this.database = "";
		this.formatquery = "";
		this.pagefoot = "";
		this.pagelist = new ArrayList<Integer>();
	}
	public PageBeanPlus(String DataBase, String sqltype, String sql,
			Integer PageSize, Integer CurrentPage,String AjaxFunction) {
		boolean T = true;
		this.database = DataBase;
		this.ajaxfunction=AjaxFunction;
		this.SqlType = sqltype;
		this.Sql = sql;
		// 开始识别初始化参数
		if (T == true) {
			if (this.database.toUpperCase().equals("ORACLE")) {
				System.out.println("识别为Oracle数据库");
			} else if (this.database.toUpperCase().equals("SQLSERVER")) {
				System.out.println("识别为Sqlserver数据库");
			} else if (this.database.toUpperCase().equals("MYSQL")) {
				System.out.println("识别为Mysql数据库");
			} else {
				System.out.println("不能识别数据库类型，操作终止~");
				T = false;
				this.WrongFormat();
			}
		}
		if (T == true) {
			if (this.SqlType.toUpperCase().equals("HQL")) {
				System.out.println("识别为hibernate语句");
			} else if (this.SqlType.toUpperCase().equals("SQL")) {
				System.out.println("识别为Sql语句");
			} else {
				System.out.println("不能识别查询语句类型，操作终止~");
				this.WrongFormat();
			}
		}
		this.Sql = sql;
		this.pagesize = PageSize;
		this.currentpage = CurrentPage;
		if (this.currentpage <= 0) {
			this.currentpage = 1;
		}
		this.pagelist = new ArrayList<Integer>();
		/*
		 * 完成基础数据的定义 需要根据数据库类型 和 查询语句类型来完成相应的配置
		 */
		if (T == true) {
			if (this.SqlType.toUpperCase().equals("HQL")
					&& this.database.toUpperCase().equals("ORACLE")) {
				System.out.println("ORACLE数据库,HQL语言查询~");
				this.Oracle_and_Hql();
			} else if (this.SqlType.toUpperCase().equals("SQL")
					&& this.database.toUpperCase().equals("ORACLE")) {
				System.out.println("ORACLE数据库,SQL语言查询~");
				this.Oracle_and_Sql();
				this.Show_PageBeanPlus();
				
			} else {
				System.out.println("不能识别的查询组合~");
			}
		}
	}
	private void Show_PageBeanPlus()
	{
		System.out.println("ORACLE数据库,SQL语言查询pagebean配置完成：");
	}
	private Integer Count_FormatQueryResultSet()
	{
		Integer p=0;
		try {
			while(this.formatqueryresultset.next())
			{
				p=p+1;
			}
			this.formatqueryresultset.beforeFirst();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return p;
	}
	private void Oracle_and_Hql()
	{
		/*
		 * 需要链接数据库，完成一次查询工作，对本类中的内容加以定义
		 * 
		 * 
		 */
	}
	@SuppressWarnings("unchecked")
	private void Oracle_and_Sql()
	{
		try {
			Jdbc_Connect_Oracle jdbc_connect_oracle=new Jdbc_Connect_Oracle();
			ResultSet rs=jdbc_connect_oracle.getStmt().executeQuery(this.Sql);
			Integer i=0;
			while(rs.next())
			{
				i=i+1;		
			}
			this.maxpage = IfRemainderPlusOne(i, this.pagesize);
			for(int j=1;j<=this.maxpage;j++)
			{
				this.pagelist.add(j);
			}
			//配置上一页和下一页的关系
			if(this.currentpage.equals(this.maxpage))
			{
				this.nextpage=this.maxpage;	
			}
			else
			{
				this.nextpage=this.currentpage+1;
			}
			if(this.currentpage.equals(1))
			{
				this.previouspage=1;
			}
			else
			{
				this.previouspage=this.currentpage-1;
			}
			//调整翻页的SQL语句
			if(this.pagesize!=0)
			{
				this.formatquery="select * from ( select row_.*, rownum rownum_ from ( "+this.Sql+")" +
					" row_ where rownum <="+this.currentpage*this.pagesize+") where rownum_ >="+((this.currentpage-1)*this.pagesize+1);			
			}
			else
			{
				this.formatquery=this.Sql;
			}
			this.formatqueryresultset=jdbc_connect_oracle.getStmt().executeQuery(this.formatquery);
			this.formatquerylist=new ArrayList();
			this.pagefoot=this.fromatpagefood();
			this.pagefoot_ajax=this.fromatpagefood_ajax(this.ajaxfunction);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private String fromatpagefood_ajax(String functionname)
	{
		//页面上必须有需要有查询ajax的回调函数,实际上通过点击翻页数据调用回调方法。从而实现翻页。
		String l="";
		for(int i=0;i<this.pagelist.size();i++)
		{
			//System.out.println(this.pagelist.get(i));
			//System.out.println(this.getCurrentpage());
			if(!this.pagelist.get(i).equals(this.getCurrentpage()))
			{
				l=l+"<option value='"+this.pagelist.get(i)+"'>"+this.pagelist.get(i)+"</option>";
			}
			else
			{
				l=l+"<option value='"+this.pagelist.get(i)+"' selected "+">"+this.pagelist.get(i)+"</option>";
			}
		}
		String pagefoot_ajax="<table width='500' border='0' cellspacing='1' align='center'" +
		"cellpadding='1' style='FONT-SIZE: 12px'> <tr><td width='20%'><span style='cursor:hand;' onclick='turnpage_ajax(1)'>首页</span></td> " +
		"<td width='20%'><span style='cursor:hand;' onclick='turnpage_ajax("+this.previouspage+")'>上一页</span></td><td width='20%' align='center'>" +
		"<input type='button' name='b1' id='b1' value='跳转' onclick='turnpage_ajax(-1)' class='btn_2k3'/><select name='page' id='page'>" +l+
		" </select> " +
		"</td><td width='20%' align='right'><span style='cursor:hand;' onclick='turnpage_ajax("+this.nextpage+")'>下一页</span></td>" +
		"<td width='20%' align='right'><span style='cursor:hand;' onclick='turnpage_ajax("+this.maxpage+")'>尾页</span></td> </tr> </table>"
		+ "<script type='text/javascript'>//需要在父网页载入的javascript为" 
		+"function turnpage_ajax(page)"
		+"{" +
				"alert('page')" +
				"if(page!=-1){"+
				"document.getElementById('currentpage').value=page;" +
				functionname +";"+
				"}else" +
				"{" +
				"document.getElementById('currentpage').value=document.getElementById('page').value;" 
				+functionname+
				";" +
				"}" +
				"}" +
				"</script>";
		return pagefoot_ajax;
	}
	private String fromatpagefood()
	{
		String l="";
		
		for(int i=0;i<this.pagelist.size();i++)
		{
			//System.out.println(this.pagelist.get(i));
			//System.out.println(this.getCurrentpage());
			if(!this.pagelist.get(i).equals(this.getCurrentpage()))
			{
				l=l+"<option value='"+this.pagelist.get(i)+"'>"+this.pagelist.get(i)+"</option>";
			}
			else
			{
				l=l+"<option value='"+this.pagelist.get(i)+"' selected "+">"+this.pagelist.get(i)+"</option>";
			}
		}
		String pagefoot="<table width='500' border='0' cellspacing='1' align='center'" +
		"cellpadding='1' style='FONT-SIZE: 12px'> <tr><td width='20%'><span style='cursor:hand;' onclick='turnpage(1)'>首页</span></td> " +
		"<td width='20%'><span style='cursor:hand;' onclick='turnpage("+this.previouspage+")'>上一页</span></td><td width='20%' align='center'>" +
		"<input type='button' name='b1' id='b1' value='跳转' onclick='turnpage(-1)' class='btn_2k3'/><select name='page' id='page'>" +l+
		" </select> " +
		"</td><td width='20%' align='right'><span style='cursor:hand;' onclick='turnpage("+this.nextpage+")'>下一页</span></td>" +
		"<td width='20%' align='right'><span style='cursor:hand;' onclick='turnpage("+this.maxpage+")'>尾页</span></td> </tr> </table>"
		+ "<script type='text/javascript'>" 
		+"function turnpage(page)"
		+"{var objForm = document.forms[0];if(page!='-1')"
		+"{objForm.currentpage.value=page;}else{objForm.currentpage.value=document.getElementById('page').value;}objForm.submit();}</script>";
		
		
		
		
			
		
		
		
			
		
		
		
		//System.out.println("配置了翻页的页脚");
		return pagefoot;
	}
	private static int IfRemainderPlusOne(double max,double size)
	{
		double ys=max% size;
		if (ys == 0)
		{
			return (int) (max / size);
			
		}
		else
		{
			return (int) Math.round(max
					/ size + 0.4999999999999999);

		}
		
	}
	public String getSqlType() {
		return SqlType;
	}

	public void setSqlType(String sqlType) {
		SqlType = sqlType;
	}

	public String getSql() {
		return Sql;
	}

	public void setSql(String sql) {
		Sql = sql;
	}

	public Integer getPagesize() {
		return pagesize;
	}

	public void setPagesize(Integer pagesize) {
		this.pagesize = pagesize;
	}

	public Integer getMaxpage() {
		return maxpage;
	}

	public void setMaxpage(Integer maxpage) {
		this.maxpage = maxpage;
	}

	public Integer getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(Integer currentpage) {
		this.currentpage = currentpage;
	}

	public Integer getNextpage() {
		return nextpage;
	}

	public void setNextpage(Integer nextpage) {
		this.nextpage = nextpage;
	}

	public Integer getPreviouspage() {
		return previouspage;
	}

	public void setPreviouspage(Integer previouspage) {
		this.previouspage = previouspage;
	}

	public List<Integer> getPagelist() {
		return pagelist;
	}

	public void setPagelist(List<Integer> pagelist) {
		this.pagelist = pagelist;
	}

	public String getDatabase() {
		return database;
	}

	public void setDatabase(String database) {
		this.database = database;
	}

	public String getFormatquery() {
		return formatquery;
	}

	public void setFormatquery(String formatquery) {
		this.formatquery = formatquery;
	}

	public String getPagefoot() {
		return pagefoot;
	}

	public void setPagefoot(String pagefoot) {
		this.pagefoot = pagefoot;
	}

	@SuppressWarnings("unchecked")
	public List getFormatquerylist() {
		return formatquerylist;
	}

	@SuppressWarnings("unchecked")
	public void setFormatquerylist(List formatquerylist) {
		this.formatquerylist = formatquerylist;
	}

	public ResultSet getFormatqueryresultset() {
		return formatqueryresultset;
	}

	public void setFormatqueryresultset(ResultSet formatqueryresultset) {
		this.formatqueryresultset = formatqueryresultset;
	}
	public String getPagefoot_ajax() {
		return pagefoot_ajax;
	}
	public void setPagefoot_ajax(String pagefoot_ajax) {
		this.pagefoot_ajax = pagefoot_ajax;
	}
	public String getAjaxfunction() {
		return ajaxfunction;
	}
	public void setAjaxfunction(String ajaxfunction) {
		this.ajaxfunction = ajaxfunction;
	}

}
	
	
	
	
	